/**
 * boson::fcontext implementation
 *
 * This code is largely salvaged from Boost.Context
 *
 * @copyright Boost
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */
#ifndef BOSON_FCONTEXT_H_
#define BOSON_FCONTEXT_H_

#ifdef BOSON_CONTEXT_DECL
#undef BOSON_CONTEXT_DECL
#endif

#if !defined(BOSON_CONTEXT_DECL)
#define BOSON_CONTEXT_DECL
#endif

#undef BOSON_CONTEXT_CALLDECL
#if (defined(i386) || defined(__i386__) || defined(__i386) || defined(__i486__) ||            \
     defined(__i586__) || defined(__i686__) || defined(__X86__) || defined(_X86_) ||          \
     defined(__THW_INTEL__) || defined(__I86__) || defined(__INTEL__) || defined(__IA32__) || \
     defined(_M_IX86) || defined(_I86_)) &&                                                   \
    defined(BOSON_WINDOWS)
#define BOSON_CONTEXT_CALLDECL __cdecl
#else
#define BOSON_CONTEXT_CALLDECL
#endif

#include <cstdint>

namespace boson {
namespace internal {

using fcontext_t = void*;

struct transfer_t {
  fcontext_t fctx;
  void* data;

  operator bool() const {
    return fctx != nullptr;
  }
};

extern "C" BOSON_CONTEXT_DECL transfer_t BOSON_CONTEXT_CALLDECL jump_fcontext(fcontext_t const to,
                                                                              void* vp);
extern "C" BOSON_CONTEXT_DECL fcontext_t BOSON_CONTEXT_CALLDECL
make_fcontext(void* sp, std::size_t size, void (*fn)(transfer_t));

// based on an idea of Giovanni Derreta
extern "C" BOSON_CONTEXT_DECL transfer_t BOSON_CONTEXT_CALLDECL
ontop_fcontext(fcontext_t const to, void* vp, transfer_t (*fn)(transfer_t));

}  // namespace internal
}  // namespace boson

#endif  // BOSON_FCONTEXT_H_
